package com.onepower.admin.controller;


import com.onepower.core.modules.repository.entity.MatchScheduleLocked;
import com.onepower.core.modules.service.IMatchScheduleLockedService;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.ResultUtils;
import com.onepower.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-08-12
 */
@RestController
@RequestMapping("/api/match/scheduleLocked")
public class MatchScheduleLockedController {

    @Autowired
    private IMatchScheduleLockedService matchScheduleLockedService;

    @Autowired
    HttpServletRequest request;

    @PostMapping(value = "lock")
    public CommonResult lock(@RequestBody MatchScheduleLocked locked) {
        matchScheduleLockedService.lockMatchSchedule(locked);
        return new CommonResult();
    }

    @PostMapping(value = "unLocked")
    public CommonResult unLocked(@RequestBody Integer[] lockIds) {
        MatchScheduleLocked matchScheduleLocked = matchScheduleLockedService.getById(lockIds[0]);
        int aId = matchScheduleLocked.getMatchAdminId();
        int uid = SecurityUtils.getCurrentUserId(request);
        if (aId == uid){
            matchScheduleLockedService.removeByIds(Arrays.asList(lockIds));
        }else {
            return ResultUtils.error(400,"无法解锁");
        }
        return ResultUtils.ok();
    }

    @PostMapping(value = "lockNextSchedule")
    public CommonResult lockNextSchedule(@RequestParam int matchId, @RequestParam int matchAdminId) {
        matchScheduleLockedService.lockNextMatchSchedule(matchId, matchAdminId);
        return new CommonResult();
    }

    @PostMapping(value = "twoTime")
    public CommonResult twoTime(@RequestBody MatchScheduleLocked matchScheduleLocked) {
        matchScheduleLockedService.lockMatchScheduleTwoTime(matchScheduleLocked);
        return new CommonResult();
    }

    @RequestMapping(value = "querySchedleLock", method = RequestMethod.POST)
    public CommonResult querySchedleLock(@RequestParam int matchId, @RequestParam int matchAdminId) {
        MatchScheduleLocked matchScheduleLocked = matchScheduleLockedService.querySchedleLock(matchAdminId, matchId);
        return ResultUtils.ok(matchScheduleLocked);
    }

}
